package com.zygame.sdk.utils.http;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.util.Log;

import com.zygame.sdk.ConstantValue;
import com.zygame.sdk.ZyGameSDK;
import com.zygame.sdk.bean.GameServerParams;
import com.zygame.sdk.bean.ZY_PayParams;
import com.zygame.sdk.model.UserModel;
import com.zygame.sdk.parser.ConfigParser;
import com.zygame.sdk.parser.JsonParser;
import com.zygame.sdk.parser.LoginParser;
import com.zygame.sdk.utils.MD5Util;
import com.zygame.sdk.utils.SharedPreferencesHelper;
import com.zygame.sdk.utils.VerifyUtil;
import com.zygame.sdk.utils.http.NetHttpUtil.DataCallback;
import com.zygame.sdk.utils.log.LogHelper;
import com.zygame.sdk.utils.phone.Phoneuitl;
import com.zygame.sdk.utils.util.Purchase;

import org.json.JSONException;
import org.json.JSONObject;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.TreeMap;

public class HttpUtil {

    /**
     * 激活接口, 在用户登录前使用，收集运行了游戏的手机信息
     *
     * @param context
     * @param callBack
     */
    public static void active(Context context, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new LinkedHashMap<String, String>();
        String action = "active";
        String gid = ConstantValue.ZY_GAMEID+"";
        String cid = ConstantValue.ZY_CID+"";
        String type = "1";
        String ext = URLEncoder.encode(getExt(context));
        ConstantValue.ZY_ext =  URLEncoder.encode(getExt(context));
        map.put(ConstantValue.ACTION, action);
        map.put(ConstantValue.GAMEID, gid);
        map.put(ConstantValue.CHANNELID, cid);
        map.put(ConstantValue.TYPE, type);
        map.put("ext", ext);
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    public static String getExt(Context context) {
        String extStr = "";
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("phone_ip", Phoneuitl.getIPAddress(context));
            jsonObject.put("phone_model", Phoneuitl.getPhoneModel());
//            jsonObject.put("phone_model", Phoneuitl.getPhoneModel());
            jsonObject.put("phone_version", Phoneuitl.getSystemVersion());
            jsonObject.put("phone_resolution", Phoneuitl.getDisplay2(context));
            LogHelper.i("分辨率", Phoneuitl.getDisplay(context));
            jsonObject.put("idfa", Phoneuitl.getIMEI(context));
            LogHelper.i("sss",Phoneuitl.getIMEI(context)+"imsi");
            jsonObject.put("ver", ConstantValue.VERSIONCODE);
            jsonObject.put("ver_name", ConstantValue.VERSIONAME);
            jsonObject.put("language", ConstantValue.ZY_LANGUAGE);
            extStr = jsonObject.toString();
            LogHelper.i("激活", extStr);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return extStr;
    }

    /**
     * @param context
     * @param callBack
     * @author K
     * @Description: TODO(获取余额)
     * @date 2014-12-20 上午11:55:16
     */
    public static void getBalance(Context context, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        String action = "get_balance";
        map.put("action", action);
        map.put("uid", UserModel.getInstance().getUid());
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.getBalanceUrl, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * 登陆
     * 旧的
     *
     * @param context
     * @param userName
     * @param passWord
     * @param callBack
     */
    public static void login(Context context, String userName, String passWord, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "login");
        map.put("login_account", userName);
        map.put("password", passWord);

        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new LoginParser()), callBack);
    }

    /**
     * 注册
     *
     * @param context
     * @param userName
     * @param passWord
     * @param callBack 如果userName和phoneNumber相同，为一键注册
     */
    public static void regist(Context context, String userName, String passWord, String phoneNum, String verifyCode, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "register");
        map.put("username", userName);
        map.put("password", passWord);
        map.put("phone", phoneNum);
        map.put("num", verifyCode); // 验证码

        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new LoginParser()), callBack);
    }

    /**
     * 获取配置
     *
     * @param context
     * @param callBack
     */
    @SuppressLint("NewApi")
    public static void requestInitConfig(Context context, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
//		map.put("action", "menuall");       //旧地址
        map.put("action", "get_menu_new");   //新地址

        NetHttpUtil.getDataFromServerGET(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new ConfigParser()), callBack);
    }

    /**
     * 获取游戏版本信息
     *
     * @param context
     * @param callBack
     */
    @SuppressLint("NewApi")
    public static void game_version(Context context, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        String gid = ConstantValue.ZY_GAMEID+"";
        String cid = ConstantValue.ZY_CID+"";
        String ext = URLEncoder.encode(getExt(context));
        String version = ConstantValue.VERSIONCODE;
        map.put("action", "game_version");
        map.put(ConstantValue.GAMEID, gid);
        map.put(ConstantValue.CHANNELID, cid);
        map.put(ConstantValue.VERSION, version + "");
        map.put("ext", ext);
        NetHttpUtil.getDataFromServerGET(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new ConfigParser()), callBack);
    }

    /**
     * 获取配置
     *
     * @param context
     * @param callBack
     */
    @SuppressLint("NewApi")
    public static void get_menu(Context context, DataCallback<JSONObject> callBack, String token) {
        HashMap<String, String> map = new HashMap<String, String>();
//		map.put("action", "menuall");       //旧地址
        //新地址
        String gid = ConstantValue.ZY_GAMEID+"";
        String cid = ConstantValue.ZY_CID+"";
        String ext = URLEncoder.encode(getExt(context));
        map.put("action", "get_menu");
        map.put(ConstantValue.GAMEID, gid);
        map.put(ConstantValue.CHANNELID, cid);
        map.put("ext", ext);
        map.put("token", token);
//        map.put("sign", sign);
        NetHttpUtil.getDataFromServerGET(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new ConfigParser()), callBack);
    }

    /**
     * 注册页面统计
     */
    public static void staRegisterPage(Context context, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "showregister");
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new ConfigParser()), callBack);
    }

    /**
     * 获取手机号绑定的账号
     *
     * @param context
     * @param callBack
     */
    public static void getAccountsFromPhoneNum(Context context, String phoneNum, DataCallback<JSONObject> callBack) {
        HashMap<String, Object> map = new LinkedHashMap<String, Object>();
        map.put("action", "get_name_byphone");
        map.put("phone", phoneNum);
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, map, new ConfigParser()), callBack);
    }

    @SuppressLint("NewApi")
    public static HashMap intiMap(HashMap<String, String> map, Context context) {
        TreeMap<String,String> treeMap = new TreeMap<String, String>();
        String signstr = "";
        for (String key : map.keySet()) {
            treeMap.put(key,map.get(key));
        }

        for(String key : treeMap.keySet()){
            if(key.equals("action")){
                signstr+=key+"="+map.get(key);
            }else if(key.equals("auth_code")){
                continue;
            } else {
                if(key.equals("user_name") || key.equals("other")){
                    signstr+="&"+key+"="+URLEncoder.encode(map.get(key));
                } else {
                    signstr+="&"+key+"="+map.get(key);
                }
            }
        }
        signstr+=ConstantValue.ZY_KEY;

        Log.i("gd", "signstr: " + signstr);
        String sign = MD5Util.getMd5(signstr);
        map.put("sign", sign);
        LogHelper.i("ssslogin",map.toString());
        return map;
    }

    /**
     * 选服
     *
     * @param context
     * @param
     * @param callBack
     */
    public static void submitServerData(Context context, String serverId, String other, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        String token = UserModel.getInstance().getLogin_Token();
        map.put("action", "gserver");
        map.put("other", other);
        map.put("token", token);
        map.put("sid", serverId);

        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * 绑定手机
     * <p>
     * 后台根据token来获取绑定的账号
     */
    public static void bindPhone(Context context, String verifyCode, String phoneNum, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "bindphone");
        map.put("cellnum", phoneNum);
        map.put("num", verifyCode);
        map.put("confirm", 1 + "");    //必须传
        map.put("token", UserModel.getInstance().getModel().getLogin_Token());
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    public static void getVerifyCodeNew(Context context, String phoneNum, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "bindphone");
        map.put("cellnum", phoneNum);
//		map.put("num", "");
//		map.put("confirm", 1+"");    //必须传
//		map.put("token", UserModel.getInstance().getModel().getToken());   
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * 获取验证码
     *
     * @param context
     * @param phoneNumber
     * @param callBack
     */
    public static void getVerifyCode(Context context, String phoneNumber, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "sendvmsg");
        map.put("cellnum", phoneNumber);
        map.put("phone_type", 1 + "");
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * 校验验证码
     *
     * @param context
     * @param phoneNum
     * @param verifyCode
     * @param callBack
     */
    public static void checkVerifyCode(Context context, String phoneNum, String verifyCode, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "checkphone_msg_new");
        map.put("cellnum", phoneNum);
        map.put("num", verifyCode);
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * 校验是否绑定手机
     *
     * @param context
     * @param name
     * @param number
     * @param callBack
     */
    public static void verifyIfBindPhone(Context context, String name, String number, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "verifycell");
        map.put("username", name);
        map.put("phone", number);
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * 手机号找回密码校验
     *
     * @param context
     * @param phoneNum
     * @param userName
     * @param verifyCode
     * @param callBack
     */
    public static void phoneNumFindPwd(Context context, String phoneNum, String userName, String verifyCode, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "checkphone_msg");
        map.put("cellnum", phoneNum);
        map.put("username", userName);
        map.put("num", verifyCode);
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }


    /**
     * 找密码(重置密码)
     *
     * @param context
     * @param
     * @param number
     * @param callBack
     */
    public static void findPassword(Context context, String userName, String number, String password, String verifyCode, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "resetpwd");
        map.put("username", userName);
        map.put("password", password);
        map.put("phone", number);
        map.put("num", verifyCode);
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * 获取版本更新信息
     *
     * @param context
     */
    public static void getVer(Context context, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "getver");
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * 获取密保问题
     *
     * @param context
     * @param callBack
     */
    public static void getQuestion(Context context, String username, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "check_safe_question");
        map.put("username", username);

        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * 确定提交
     */
    public static void confirmQuestion(Context context, String username, String answer, String newPwd, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "safe_question");
        map.put("username", username);
        map.put("app_key", VerifyUtil.getAppkey(context));
        map.put("answer", answer);
        map.put("pwd", newPwd);

        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * 一键注册，获取账号
     *
     * @param context
     * @param callBack
     */
    public static void getAccount(Context context, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "getname");

        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * 重设密码
     *
     * @param context
     * @param name
     * @param password
     * @param callBack
     */
    public static void resetPassword(Context context, String name, String password, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "resetpwd");
        map.put("username", name);
        map.put("password", password);
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * 重设密码
     * 必须get
     */
    public static void resetPasswordNew(Context context, String verifyCode, String phone, String password, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "resetpwd_new");
        map.put("num", verifyCode);   //校验码
        map.put("password", password);
        map.put("phone", phone);
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * @author
     * @Description: TODO(註銷)
     * @date 2014-11-27 上午11:13:36
     */
    public static void exitAccount(Context context, String token) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "logout");
        map.put("token", token);
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), null);
    }

    /**
     * 获取版本信息
     *
     * @param context
     */
    public static void getVersion(Context context, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new HashMap<String, String>();
        map.put("action", "getversion");
        map.put("ver", ZyGameSDK.getInstance().getSDKVersion());
        NetHttpUtil.getDataFromServerGET(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    public static String urlSplice(String action, String url, HashMap<String, String> map, Context context) {
        map.put("action", action); // action一定要put进去
        intiMap(map, context);

        for (String key : map.keySet()) {
            if (!"action".equals(key)) {
                url += ("&" + key + "=" + map.get(key));
            }
        }

        return url;
    }

    /**
     * 所有api接口都要加sign sign
     * 规则：將所有get或post的參數，已action参数开头，在按请求参数的字母排序,最后加privatekey
     * （privatekey前面不需要加　&），然后md5加密字符串
     *
     * @param action
     * @param map
     * @param context
     * @return
     */
    public static String encryption(String action, TreeMap<String, String> map, Context context) {
        String sign = null;
        String keyString = action;
        String privatekey = ZyGameSDK.getInstance().getPrivateKey();

        for (String key : map.keySet()) {
            keyString += "&" + map.get(key);
        }
        LogHelper.i("sss",keyString+"");
        keyString += privatekey;
        sign = MD5Util.getMd5toLowerCase(keyString);

        return sign;
    }

    // 用户名是否格式良好
//	public static boolean isUserNameCorrect(String userName) {
//		if (!TextUtils.isEmpty(userName)) {
//			Pattern pattern = Pattern.compile("^[0-9a-zA-Z_]{4,20}$");
//			Matcher matcher = pattern.matcher(userName);
//			return matcher.find();
//		}
//		return false;
//	}
//
//	// 验证密码是否格式良好
//	public static boolean isPasswordCorrect(String password) {
//		if (!TextUtils.isEmpty(password)) {
//			Pattern pattern = Pattern.compile("^[0-9a-zA-Z_]{6,20}$");
//			Matcher matcher = pattern.matcher(password);
//			return matcher.find();
//		}
//		return false;
//	}


    /**
     * 注册
     *
     * @param context
     * @param
     */
    public static void reg(Context context, DataCallback<JSONObject> callBack, String name, String pwd, String type) {
        HashMap<String, String> map = new LinkedHashMap<String, String>();
        String action = "reg";
        String gid = ConstantValue.ZY_GAMEID+"";
        String cid = ConstantValue.ZY_CID+"";
        String ext = URLEncoder.encode(getExt(context));
        String token = SharedPreferencesHelper.getInstance().getToken(context);


        map.put(ConstantValue.ACTION, action);
        map.put(ConstantValue.GAMEID, gid);
        map.put(ConstantValue.CHANNELID, cid);
        map.put(ConstantValue.TYPE, type);
        map.put("user_name", name);
        map.put("password", pwd);
        map.put("token", token);
        map.put("ext", ext);

        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);

    }


    /**
     * Google支付完成
     * @param purchase
     * @param params
     * @param context
     */
    public static void googlePay(Purchase purchase, ZY_PayParams params, Activity context,DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new LinkedHashMap<String, String>();
        String gid = ConstantValue.ZY_GAMEID + "";
        String cid = ConstantValue.ZY_CID + "";
        String sid = ConstantValue.ZY_SERVER_AUTHCODE;

        String game_order_id = URLEncoder.encode(params.getGame_order_id());
        String game_other = URLEncoder.encode(params.getGame_other());
        final String goods_id = params.getGoods_id();
        String goods_name = URLEncoder.encode(params.getGoods_name());
        double goods_price = params.getGoods_price();
        String sandbox = params.getSandbox();
        int pay_num = params.getPay_num();
        String pay_money = pay_num * goods_price + "";
        String ext = URLEncoder.encode(getExt(context));
        String token = SharedPreferencesHelper.getInstance().getToken(context);

        map.put(ConstantValue.GAMEID, gid);
        map.put(ConstantValue.CHANNELID, cid);
        map.put(ConstantValue.SERVICEID, sid);
        map.put("receipt", purchase.getOriginalJson());
        map.put("token", token);
        map.put("login_token", SharedPreferencesHelper.getInstance().getLogin_Token(context));
        map.put("game_order_id", game_order_id);
        map.put("game_other", game_other);
        map.put("sandbox", sandbox);
        map.put("goods_id", goods_id);
        map.put("goods_name", goods_name);
        map.put("goods_price", String.valueOf(goods_price));
        map.put("pay_money", pay_money);
        map.put("ext", ext);

        Log.i("GD", "googlePay: " + map.toString());
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL + "/google", intiMap(map, context), new JsonParser()), callBack);
    }


    /**
     * 获取随机用户名
     *
     * @param context
     * @param callBack
     */
    public static void getName(Context context, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new LinkedHashMap<String, String>();
        String action = "get_name";
        String gid = ConstantValue.ZY_GAMEID+"";
        String cid = ConstantValue.ZY_CID+"";

        String ext = URLEncoder.encode(getExt(context));
        String token = SharedPreferencesHelper.getInstance().getToken(context);
        map.put(ConstantValue.ACTION, action);
        map.put(ConstantValue.GAMEID, gid);
        map.put(ConstantValue.CHANNELID, cid);
        map.put("token", token);
        map.put("ext", ext);

        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }


    /**
     * 登录接口
     *  @param name
     * @param pwd
     * @param type
     * @param authCode Google登录code
     * @param context
     * @param callBack
     */
    public static void login(String name, String pwd, String authCode,String type, String otherstr,  Context context, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new LinkedHashMap<String, String>();
        String action = "login";
        String gid = ConstantValue.ZY_GAMEID+"";
        String cid = ConstantValue.ZY_CID+"";
        String ext = URLEncoder.encode(getExt(context));
        String token = SharedPreferencesHelper.getInstance().getToken(context);
        map.put(ConstantValue.ACTION, action);
        map.put(ConstantValue.GAMEID, gid);
        map.put(ConstantValue.CHANNELID, cid);
        map.put(ConstantValue.TYPE, type);
        map.put("user_name", name);
        map.put("password", pwd);
        map.put("other", otherstr);
        map.put("token", token);
        map.put("auth_code", authCode);
        map.put("ext", ext);

        Log.i("GD", "login map: " + map.toString());

        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    /**
     * 用户登录注销
     *
     * @param context
     * @param callBack
     */
    public static void logout(Context context, DataCallback<JSONObject> callBack) {
        HashMap<String, String> map = new LinkedHashMap<String, String>();
        String action = "logout";
        String gid = ConstantValue.ZY_GAMEID+"";
        String cid = ConstantValue.ZY_CID+"";
        String ext = URLEncoder.encode(getExt(context));
        String token = SharedPreferencesHelper.getInstance().getToken(context);
//        String login_token = context.getSharedPreferences("tokeninfo", 0).getString("login_token", "");
        String login_token = SharedPreferencesHelper.getInstance().getLogin_Token(context);


        map.put(ConstantValue.ACTION, action);
        map.put(ConstantValue.GAMEID, gid);
        map.put(ConstantValue.CHANNELID, cid);
        map.put("token", token);
        map.put("login_token", login_token);
        map.put("ext", ext);

        Log.i("GD", "logout map: " + map.toString());
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }




    /**
     * 游戏角色数据
     *
     * @param context
     * @param callBack
     */
    public static void game_server(Context context, GameServerParams params ,DataCallback<JSONObject> callBack) {

//        String roleNme, String serverName, String roleLevel, String serverId, String roleId,
        HashMap<String, String> map = new LinkedHashMap<String, String>();
        String action = "game_server";
        String gid = ConstantValue.ZY_GAMEID + "";
        String cid = ConstantValue.ZY_CID + "";
        String game_role = "";
        SharedPreferencesHelper.getInstance().setSid(context,params.getServerId());
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("role_name", params.getRoleName());
            jsonObject.put("server_name", params.getServerName());
            jsonObject.put("role_level", params.getRoleLevel());
            jsonObject.put("server_id", params.getServerId());
            jsonObject.put("role_id", params.getRoleId());
           SharedPreferencesHelper.getInstance().setGameJson(context,jsonObject.toString());
/*//测试
            jsonObject.put("role_name", "测试");
            jsonObject.put("server_name", "测试");
            jsonObject.put("role_level", "99");
            jsonObject.put("server_id", "1");
            jsonObject.put("role_id", "1");
*/
            game_role = URLEncoder.encode(jsonObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        String ext = URLEncoder.encode(getExt(context));
        String token = SharedPreferencesHelper.getInstance().getToken(context);
//        String login_token = context.getSharedPreferences("tokeninfo", 0).getString("login_token", "");
        String login_token = SharedPreferencesHelper.getInstance().getLogin_Token(context);


        map.put(ConstantValue.ACTION, action);
        map.put(ConstantValue.GAMEID, gid);
        map.put(ConstantValue.CHANNELID, cid);
        map.put("game_ext", game_role);

        map.put("login_token", login_token);
        map.put("ext", ext);
        map.put("token", token);
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }

    public static void game_online_time(Context context, DataCallback<JSONObject> callBack,String gamejson,String login_token,String token) {
        HashMap<String, String> map = new LinkedHashMap<String, String>();
        String action = "game_online_time";
        String gid = ConstantValue.ZY_GAMEID+"";
        String cid = ConstantValue.ZY_CID+"";
        String game_role = URLEncoder.encode(gamejson);
//        try {
//            JSONObject jsonObject = new JSONObject();
//            jsonObject.put("role_name", "测试");
//            jsonObject.put("server_name", "测试");
//            jsonObject.put("role_level", "99");
//            jsonObject.put("server_id", "1");
//            jsonObject.put("role_id", "1");
//            game_role = URLEncoder.encode(jsonObject.toString());
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }

        String ext = URLEncoder.encode(getExt(context));


        map.put(ConstantValue.ACTION, action);
        map.put(ConstantValue.GAMEID, gid);
        map.put(ConstantValue.CHANNELID, cid);
        map.put("game_online_time", game_role);

        map.put("login_token", login_token);
        map.put("ext", ext);
        map.put("token", token);
        NetHttpUtil.getDataFromServerPOST(context, new RequestModel(ConstantValue.URL, intiMap(map, context), new JsonParser()), callBack);
    }


    // public String   getSin( HashMap<String, String> map ){
//     TreeMap<String,String> treeMap = new TreeMap<String, String>();
//     String action = null;
//     for (String key : map.keySet()) {
//         if(key.equals("action")){
//             action = map.get(key);
//         }else{
//             treeMap.put(key, map.get(key));
//         }
//     }
//     encryption();
////     encryption(String action, TreeMap<String, String> map, Context context)
//     return "";
// }


}
